exports.__esModule = true;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _sequelize = require('sequelize');

var _sequelize2 = _interopRequireDefault(_sequelize);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * ORM模型装载基类
 */
class ModelMapping {

    static initModelMapping() {
        const sequelConfig = think.config('sequelConfig');
        let db = {}; //需要挂载db对象
        const database = sequelConfig.database;
        const username = sequelConfig.username;
        const password = sequelConfig.password;
        const sequelize = new _sequelize2.default(database, username, password, sequelConfig);
        _fs2.default.readdirSync(_path2.default.join(think.ROOT_PATH, 'src', 'model')).filter(function (file) {
            return file.indexOf(".") !== 0;
        }).forEach(function (file) {
            let model = sequelize.import(_path2.default.join(think.ROOT_PATH, 'src', 'model', file));
            db[model.name] = model;
        });
        Object.keys(db).forEach(function (modelName) {
            if ("associate" in db[modelName]) {
                db[modelName].associate(db);
            }
        });
        db.sequelize = sequelize;
        db.Sequelize = _sequelize2.default;
        return db;
    }
}

exports.default = ModelMapping;